package com.mobiletrain.org.motherkitch.fenlei;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.fenlei.bean.SecondItem;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class AfragmentSecondActivity extends ActionBarActivity {
    private ArrayList<SecondItem> seconds = new ArrayList<SecondItem>();
    private SecondItem second;
    private PullToRefreshListView view;
    private BitmapUtils bitmapUtils;
    private ListView secondlist;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_afragment_second);
        bitmapUtils = ImageLoad.getBitmapUtils(this);
        view = (PullToRefreshListView) findViewById(R.id.second_list);
        secondlist=view.getRefreshableView();
        innit(view);
        Intent i = getIntent();
        Bundle bundle = i.getBundleExtra("key");
        id = bundle.getInt("id");
        String name = bundle.getString("name");
        TextView tooltext = (TextView) findViewById(R.id.toolbarTitle);
        tooltext.setText(name);
        ImageView toolimg = (ImageView) findViewById(R.id.toolbarImage);
        Toolbar toolbar = (Toolbar) findViewById(R.id.second_toolbar);
        setSupportActionBar(toolbar);

        toolimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AfragmentSecondActivity.this.finish();
            }
        });
        secondlist.setAdapter(new MyAdapter1());

        secondjson(id);
        secondlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent();
                i.setClass(AfragmentSecondActivity.this,ThridActivity.class);
                Bundle bundle1=new Bundle();
                bundle1.putString("id",seconds.get(position-1).getId());
                bundle1.putString("pic",seconds.get(position-1).getPic());
                bundle1.putString("title",seconds.get(position-1).getTitle());
                bundle1.putString("cai",seconds.get(position-1).getFood_str());
                bundle1.putString("num",seconds.get(position-1).getNum());
                i.putExtra("key",bundle1);
                startActivity(i);
            }
        });
    }

    private void secondjson(int id1) {
        String path = "http://www.qubaobei.com/ios/cf/dish_list." +
                "php?stage_id=" + id1 + "&page=1&limit=20";
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String json = objectResponseInfo.result.toString();
                try {
                    JSONObject obj = new JSONObject(json);
                    JSONArray array = obj.getJSONArray("data");
                    for (int j = 0; j < array.length(); j++) {
                        second = new SecondItem();
                        JSONObject obje = array.getJSONObject(j);
                        second.setId(obje.getString("id"));
                        second.setTitle(obje.getString("title"));
                        second.setPic(obje.getString("pic"));
                        second.setNum(obje.getString("collect_num"));
                        second.setFood_str(obje.getString("food_str"));
                        seconds.add(second);
                    }
                    secondlist.setAdapter(new MyAdapter1());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void innit(final PullToRefreshListView view) {
        view.setMode(PullToRefreshBase.Mode.BOTH);
        view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      view.onRefreshComplete();
                    }
                },2000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      view.onRefreshComplete();
                    }
                },2000);
                secondjson(id++);
            }
        });
    }


    class MyAdapter1 extends BaseAdapter {


        @Override
        public int getCount() {
            if (seconds != null) {
                return seconds.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return seconds.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHoder1 hoder1 = null;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.secondlist_item, null);
                hoder1 = new MyHoder1();
                hoder1.icon = (ImageView) convertView.findViewById(R.id.second_list_item_icon);
                hoder1.title = (TextView) convertView.findViewById(R.id.second_list_item_title);
                hoder1.food_str = (TextView) convertView.findViewById(R.id.second_list_item_foot);
                hoder1.num = (TextView) convertView.findViewById(R.id.second_list_item_nul);
                convertView.setTag(hoder1);
            } else {
                hoder1 = (MyHoder1) convertView.getTag();
            }
            SecondItem second = seconds.get(position);
            bitmapUtils.display(hoder1.icon, second.getPic());
            hoder1.title.setText(second.getTitle());
            hoder1.food_str.setText(second.getFood_str());
            hoder1.num.setText(second.getNum() + "人以收藏");

            return convertView;
        }
    }

    class MyHoder1 {
        ImageView icon;
        TextView title;
        TextView food_str;
        TextView num;

    }


}
